/*
 * $Id: PendingChangelistsDropAdapter.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.ui.dnd;

import java.lang.reflect.InvocationTargetException;

import net.sourceforge.p4eclipse.core.PerforceException;
import net.sourceforge.p4eclipse.core.api.IChangesResult;
import net.sourceforge.p4eclipse.core.api.IInfoResult;
import net.sourceforge.p4eclipse.core.api.IOpenedResult;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.widgets.Shell;

/**
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class PendingChangelistsDropAdapter extends ViewerDropAdapter
{
	private TransferData currentTransfer;
	private Object currentTarget;
	
	/** @see ViewerDropAdapter#ViewerDropAdapter(org.eclipse.jface.viewers.Viewer) */
	public PendingChangelistsDropAdapter(Viewer viewer)
	{
		super(viewer);
	}

	/** @see ViewerDropAdapter#performDrop(Object) */
	public boolean performDrop(Object data)
	{
		boolean result = false;
		
		if (OpenedResultTransfer.getInstance().isSupportedType(currentTransfer))
		{
			IOpenedResult[] openedResult = (IOpenedResult[])data;
			
			String[] filenames = new String[openedResult.length];
			for (int i = 0, length = openedResult.length; i < length; i++)
				filenames[i] = openedResult[i].getDepotFilename();
			
			IChangesResult changesResult = (IChangesResult)currentTarget;
			if (changesResult != null)
			{
				try
				{
					PerforceUIPlugin.getClient().reopen(changesResult.getChangelist(), filenames);
					getViewer().refresh();
				}
				catch (PerforceException e)
				{
					ErrorDialog.openError(null, "Perforce", "p4 reopen failed.", e.getStatus());
				}
			}
		}
		
		return result;		
	}

	/** @see ViewerDropAdapter#validateDrop(Object, int, TransferData) */
	public boolean validateDrop(Object target, int operation, TransferData transferType)
	{
		currentTransfer = transferType;
		
		if (target instanceof IChangesResult)
		{
			IChangesResult changesResult = (IChangesResult)target;
			
			IInfoResult infoResult = PerforceUIPlugin.getClient().info();
			if (!(infoResult.getClientName().equals(changesResult.getClientName())))
			{
				currentTarget = null;
				return false;			
			}
			
			currentTarget = target;		
			return true;
		}
		else
		{
			currentTarget = null;
			return false;			
		}
	}

	/** Returns the shell */
	private Shell getShell() 
	{
		return getViewer().getControl().getShell();
	}
}
